package order.infrastructure.util;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import order.application.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PayUtil {
    @Autowired
    private OrderService orderService;
    //appid
    private final String APP_ID = "2021000148686164";
    //应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCpHmCKAI9SXN612laWGKs3rHNTktQqYV5oecX4kmlGOxRSV111c/F4uVkQK4YePYzGhCuvTwhVXkEs4tQcxVcUWZVGfOs8uVNNntE8QmFXeAsGweyhhA0kLiLSZfC15DoObwEbJpgzpDzHRluuJtpRw22Dsu/jVNsZVcKUNbW8W7cZ3s6YWhFS1+sm3gSsX3/zG2VEoHHVe7NA2xKJQEazG6bAlflR9Sx9Jqoe0bkYPXUjky1jKkVa8hPRBA7YhYnt/IwqHXbAkWPmkz5a0d1zRS81PrFJ23Xvg3/UZbk9dvcDn1GA0RQ/HbrwlCDn62ujAOTNCArHgDzZOqtOWSuFAgMBAAECggEARTWNRGyeOpKPAdTsglhuGUaVm6oG8aEPRaov/l3Mf1VGJ7xhHuPn+gPAEwH8zPtbVRJZ3lB1lJoSChB5xZxs2oMet83/ZsZg9O79qa9wrp8NxCIweJjV1+yZi4j0TTutFqMOzNGqj9eyl8qPUmHMDpHvi2n7Cgl78I5MJuMkEIS64N/wSsLcxxfnC/Tm+0H6bT6yVdlQWesgUyj5NFHOocmTSDD54W55ghHX2rjEn9bwDjs9/u0FI1joGGLqWw986S5W7O51rLdHHTPH1Jtxj0Daem/WmkwJjDnuQceJuw9VxeW6EeVtvMRtWdLTIVJ1GvuqD1Z2bjF8/wmWD6a0AQKBgQD6NKsxCCjNG1MHWE1nR7i+tUQI6k8ORCJADHFdsL6ifSXi2cHqMXG/NFEMIgvNOurMM/ozcE+ixh33qMOSjWzJYmgJdaIHmjn7oYxrPnY68UclTnRojCxeQCFmyx/VVccys8ntTywSwOuzdERZ4+tEqF695TTnuwPkTU7c3Nz7QQKBgQCtCPzuD2Ev9mo4L+MrzgUyfgMLD7d2ZeewYt27vnoNtQ+QpYmMmmuTAGmMTfBYmMFFC3AVOhuxqL0+xkpPVglxKWMaocx3Z5UvVoYbw7W4Uj5k9/zJdxAmE5yklq0iiMxHmkpM3ofNIGdPFzVFZD0G5MluAdIW0yOy1p2aM1uzRQKBgQD2esYeQkx3abCMbseps7TiYg1IvxHsGuCmudtAEapqxeB5rkziWZ0VYANrcWqw3ZaKXbXf0IcS8hp5bA5lo96EMOYOU4yv8pWmNonu6BRukluk+7mfeF8I5iJBcl7EuYDHiOemWLUxME3BdgsThnyxITBb350Tn18BG8tRtl4XQQKBgQCeNx2uPvnKdTHidkbtgs9Vv8rgjlpeNAeJTJ0aUawgZHH6/KGsonXCUBoyZAFjwSjXDQJLJnq0E2oWwBCqQi0X/5PSgicG9vt98Utmj6jVBL5wbCADxo5Jgp00EWH1tcezoOxXJRJfCUXtF3dqH6F9cCyOHbQPkreNipdIq7UpUQKBgQCmzTh6XExIYften5vS/L8hYOYaVC1F/JnzDX5b+2k9jVJXuw8TF52FI2Udcj7ugvQo6ltQ76VzpNljQigbcNTdenABTJOPtZXpfiqi6hn5b2QiVCLbNOTxOefEQiG1WEIvCEOWqPx6NWUv9Kd7FavMFC01yLxI4TnWrWwk2v7J9w==";

    private final String CHARSET = "UTF-8";
    // 支付宝公钥
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5Mw9rgZYsL9vFVZh7EF0oU6GI2w3aEMg75tSej8M+CVLgbX1zRWTjou3ldZMPzKTd8DYgR6WmXgI/4OL2FNGCktGACgeyxHiawAKJLWDvmq93V+sFM2Eh4785hvZ7IoadFOhex9ErXtAVmlNvhc0vHi5nMAmchcNgvusUwPySiZG6oA6A1eJQ876zUlpIYpm0o5xgoeZtkzt51IT88RR+oSc5R7KS0Xgmj49GjtOJpy4gxouJEvt2RlxjEtCn6lx0YzORdS/ALUa6lJSwk6SZcHvChl+E/AuVzkFhHRw6ms1YN6a3JB/nW2KlYEiZMFMSIGP8P/U63O+k2uoLPUjGwIDAQAB";
    //沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://xht685.natappfree.cc/api/alipay/toSuccess";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:8080/api/alipay/toSuccess";
    private AlipayClient alipayClient = null;
    //支付宝官方提供的接口
    public String sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject) throws AlipayApiException {
        //获得初始化的AlipayClient
        alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(RETURN_URL);
        alipayRequest.setNotifyUrl(NOTIFY_URL);

        //商品描述（可空）
        String body = "";
        alipayRequest.setBizContent("{\"out_trade_nos\":\"" + outTradeNo + "\","
                + "\"total_amount\":\"" + totalAmount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("返回的结果是："+result );
        return result;
    }

    //    通过订单编号查询
    public String query(String id){
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        String body=null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return body;
    }
}

